﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    public class MouseEvent
    {
        public static MouseState PreMouseState;
        public static void MouseAllEvents(MouseState CurrentState)
        {
            if (PreMouseState.LeftButton == ButtonState.Pressed && CurrentState.LeftButton == ButtonState.Released)
            {
                MouseLeftEvent(CurrentState);
            }
            if (PreMouseState.RightButton == ButtonState.Pressed && CurrentState.RightButton == ButtonState.Released)
            {
                MouseRightEvent();
            }
            

        }
        public static void MouseLeftEvent(MouseState CurrentState)
        {

                if (Coordinate.selectedX >= 0 && Coordinate.selectedY >= 0 && Coordinate.selectedX < GameBoardProperty.tiles.GetLength(0) && Coordinate.selectedY < GameBoardProperty.tiles.GetLength(1))
                {
                    if (GameBoardState.State == GameBoardState.GameState.Selection)
                    {
                        GameBoardProperty.SelectedTile = GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY];
                        if (GameBoardProperty.SelectedTile.Unit != null && GameBoardProperty.Players[GameBoardProperty.PlayerControlIndex].Units.IndexOf(GameBoardProperty.SelectedTile.Unit) !=-1)
                        {
                            GameBoardState.State = GameBoardState.GameState.Move;
                            GameBoardProperty.ReachTiles.Clear();
                            GameBoardProperty.AllReachTiles(GameBoardProperty.SelectedTile, GameBoardProperty.SelectedTile.Unit, GameBoardProperty.SelectedTile.Unit.Move_Remain);
                            GameBoardProperty.ReachTiles = GameBoardProperty.ReachTiles.Distinct().ToList<Tile>();
                        }
                    }
                    else if (GameBoardState.State == GameBoardState.GameState.Move)
                    {
                        if (GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit != null)
                        {
                            if (GameBoardProperty.Players[GameBoardProperty.PlayerControlIndex].Units.IndexOf(GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit) != -1)
                            {
                                if (GameBoardProperty.SelectedTile.Unit == GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit)
                                {
                                    GameBoardState.State = GameBoardState.GameState.Attack;
                                }
                                else
                                {
                                    GameBoardProperty.SelectedTile = GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY];
                                    GameBoardProperty.ReachTiles.Clear();
                                    GameBoardProperty.ReachTiles = GameBoardProperty.ReachTiles.Distinct().ToList<Tile>();
                                    GameBoardProperty.AllReachTiles(GameBoardProperty.SelectedTile, GameBoardProperty.SelectedTile.Unit, GameBoardProperty.SelectedTile.Unit.Move_Remain);
                                }
                            }
                        }
                        else if (GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY]._LandPossible && GameBoardProperty.ReachTiles.IndexOf(GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY]) != -1)
                        {
                            GameBoardProperty.SelectedTile.Unit.Move_Remain -= GameBoardProperty.CheckDistance(GameBoardProperty.SelectedTile._Position, new Vector2(Coordinate.selectedX, Coordinate.selectedY));

                            if (GameBoardProperty.SelectedTile._Position.X < Coordinate.selectedX)
                            {
                                GameBoardProperty.SelectedTile.Unit.FaceLeft = false;
                            }
                            else if (GameBoardProperty.SelectedTile._Position.X > Coordinate.selectedX)
                            {
                                GameBoardProperty.SelectedTile.Unit.FaceLeft = true;
                            }

                            GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit = GameBoardProperty.SelectedTile.Unit;
                            GameBoardProperty.SelectedTile.Unit = null;
                            GameBoardProperty.SelectedTile = null;

                            GameBoardState.State = GameBoardState.GameState.Selection;
                        }
                    }
                    else if (GameBoardState.State == GameBoardState.GameState.Attack)
                    {
                        if (GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit == null)
                        {
                            GameBoardProperty.SelectedTile = GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY];
                            GameBoardState.State = GameBoardState.GameState.Selection;
                        }
                        else if (GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit == GameBoardProperty.SelectedTile.Unit)
                        {
                            GameBoardState.State = GameBoardState.GameState.Move;
                        }
                        else if (GameBoardProperty.Players[GameBoardProperty.PlayerControlIndex].Units.IndexOf(GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit) != -1)
                        {
                            GameBoardProperty.SelectedTile = GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY];
                            GameBoardState.State = GameBoardState.GameState.Move;
                        }
                        else
                        {
                            GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit.HP -= GameBoardProperty.CheckDamage(GameBoardProperty.SelectedTile.Unit, GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit);
                            GameBoardProperty.SelectedTile.Unit.HP -= 5;

                            if (GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit.HP <= 0)
                            {
                                GameBoardProperty.tiles[Coordinate.selectedX, Coordinate.selectedY].Unit = null;
                            }
                            if (GameBoardProperty.SelectedTile.Unit.HP <= 0)
                            {
                                GameBoardProperty.SelectedTile.Unit = null;
                            }

                            GameBoardProperty.SelectedTile = null;
                            GameBoardState.State = GameBoardState.GameState.Selection;
                        }
                    }
                }
           
        }

        public static void MouseRightEvent()
        {
            GameBoardProperty.PlayerControlIndex++;
            if (GameBoardProperty.PlayerControlIndex > GameBoardProperty.players - 1)
            {
                GameBoardProperty.PlayerControlIndex = 0; // back to first player
            }
            foreach (Unit unit in GameBoardProperty.Players[GameBoardProperty.PlayerControlIndex].Units)
            {
                unit.Move_Remain = unit.Move_Range;
            }

            GameBoardState.State = GameBoardState.GameState.Selection;
        }

    }
}
